home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing;
-
- import com.sun.java.accessibility.AccessibleContext;
- import com.sun.java.swing.text.AttributeSet;
- import com.sun.java.swing.text.BadLocationException;
- import com.sun.java.swing.text.Document;
- import com.sun.java.swing.text.Element;
- import com.sun.java.swing.text.JTextComponent;
- import com.sun.java.swing.text.PlainDocument;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Rectangle;
-
- public class JTextArea extends JTextComponent {
- private int rows;
- private int columns;
- private int columnWidth;
- private int rowHeight;
- private boolean wrap;
- private boolean word;
-
- public JTextArea() {
- this((Document)null, (String)null, 0, 0);
- }
-
- public JTextArea(int rows, int columns) {
- this((Document)null, (String)null, rows, columns);
- }
-
- public JTextArea(Document doc) {
- this(doc, (String)null, 0, 0);
- }
-
- public JTextArea(Document doc, String text, int rows, int columns) {
- this.rows = rows;
- this.columns = columns;
- if (doc == null) {
- doc = this.createDefaultModel();
- }
-
- ((JTextComponent)this).setDocument(doc);
- if (text != null) {
- ((JTextComponent)this).setText(text);
- }
-
- }
-
- public JTextArea(String text) {
- this((Document)null, text, 0, 0);
- }
-
- public JTextArea(String text, int rows, int columns) {
- this((Document)null, text, rows, columns);
- }
-
- public void append(String str) {
- Document doc = ((JTextComponent)this).getDocument();
- if (doc != null) {
- try {
- doc.insertString(doc.getLength(), str, (AttributeSet)null);
- } catch (BadLocationException var3) {
- }
- }
-
- }
-
- protected Document createDefaultModel() {
- return new PlainDocument();
- }
-
- public AccessibleContext getAccessibleContext() {
- if (super.accessibleContext == null) {
- super.accessibleContext = new AccessibleJTextArea(this);
- }
-
- return super.accessibleContext;
- }
-
- public int getColumns() {
- return this.columns;
- }
-
- protected int getColumnWidth() {
- if (this.columnWidth == 0) {
- FontMetrics metrics = ((Component)this).getFontMetrics(((Component)this).getFont());
- this.columnWidth = metrics.charWidth('m');
- }
-
- return this.columnWidth;
- }
-
- public int getLineCount() {
- Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
- return map.getElementCount();
- }
-
- public int getLineEndOffset(int line) throws BadLocationException {
- Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
- if (line < 0) {
- throw new BadLocationException("Negative line", -1);
- } else if (line >= map.getElementCount()) {
- throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
- } else {
- Element lineElem = map.getElement(line);
- return lineElem.getEndOffset();
- }
- }
-
- public int getLineOfOffset(int offset) throws BadLocationException {
- Document doc = ((JTextComponent)this).getDocument();
- if (offset < 0) {
- throw new BadLocationException("Can't translate offset to line", -1);
- } else if (offset > doc.getLength()) {
- throw new BadLocationException("Can't translate offset to line", doc.getLength() + 1);
- } else {
- Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
- return map.getElementIndex(offset);
- }
- }
-
- public int getLineStartOffset(int line) throws BadLocationException {
- Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
- if (line < 0) {
- throw new BadLocationException("Negative line", -1);
- } else if (line >= map.getElementCount()) {
- throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
- } else {
- Element lineElem = map.getElement(line);
- return lineElem.getStartOffset();
- }
- }
-
- public boolean getLineWrap() {
- return this.wrap;
- }
-
- public Dimension getMinimumSize() {
- return this.columns == 0 && this.rows == 0 ? super.getMinimumSize() : this.getPreferredSize();
- }
-
- public Dimension getPreferredScrollableViewportSize() {
- Dimension size = super.getPreferredScrollableViewportSize();
- size = size == null ? new Dimension(400, 400) : size;
- size.width = this.columns == 0 ? size.width : this.columns * this.getColumnWidth();
- size.height = this.rows == 0 ? size.height : this.rows * this.getRowHeight();
- return size;
- }
-
- public Dimension getPreferredSize() {
- Dimension d = super.getPreferredSize();
- d = d == null ? new Dimension(400, 400) : d;
- if (this.columns != 0) {
- d.width = Math.max(d.width, this.columns * this.getColumnWidth());
- }
-
- if (this.rows != 0) {
- d.height = Math.max(d.height, this.rows * this.getRowHeight());
- }
-
- return d;
- }
-
- protected int getRowHeight() {
- if (this.rowHeight == 0) {
- FontMetrics metrics = ((Component)this).getFontMetrics(((Component)this).getFont());
- this.rowHeight = metrics.getHeight();
- }
-
- return this.rowHeight;
- }
-
- public int getRows() {
- return this.rows;
- }
-
- public boolean getScrollableTracksViewportWidth() {
- return this.wrap;
- }
-
- public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
- switch (orientation) {
- case 0:
- return this.getColumnWidth();
- case 1:
- return this.getRowHeight();
- default:
- throw new IllegalArgumentException("Invalid orientation: " + orientation);
- }
- }
-
- public int getTabSize() {
- int size = 8;
- Document doc = ((JTextComponent)this).getDocument();
- if (doc != null) {
- Integer i = (Integer)doc.getProperty("tabSize");
- if (i != null) {
- size = i;
- }
- }
-
- return size;
- }
-
- public String getUIClassID() {
- return "TextAreaUI";
- }
-
- public boolean getWrapStyleWord() {
- return this.word;
- }
-
- public void insert(String str, int pos) {
- Document doc = ((JTextComponent)this).getDocument();
- if (doc != null) {
- try {
- doc.insertString(pos, str, (AttributeSet)null);
- } catch (BadLocationException var5) {
- throw new IllegalArgumentException(((Throwable)var5).getMessage());
- }
- }
-
- }
-
- public boolean isManagingFocus() {
- return true;
- }
-
- protected String paramString() {
- return super.paramString() + ",rows=" + this.rows + ",columns=" + this.columns;
- }
-
- public void replaceRange(String str, int start, int end) {
- if (end < start) {
- throw new IllegalArgumentException("end before start");
- } else {
- Document doc = ((JTextComponent)this).getDocument();
- if (doc != null) {
- try {
- doc.remove(start, end - start);
- doc.insertString(start, str, (AttributeSet)null);
- } catch (BadLocationException var6) {
- throw new IllegalArgumentException(((Throwable)var6).getMessage());
- }
- }
-
- }
- }
-
- public void setColumns(int columns) {
- int oldVal = this.columns;
- if (columns < 0) {
- throw new IllegalArgumentException("columns less than zero.");
- } else {
- if (columns != oldVal) {
- this.columns = columns;
- ((Container)this).invalidate();
- }
-
- }
- }
-
- public void setFont(Font f) {
- super.setFont(f);
- this.rowHeight = 0;
- this.columnWidth = 0;
- ((JComponent)this).revalidate();
- }
-
- public void setLineWrap(boolean wrap) {
- boolean old = this.wrap;
- this.wrap = wrap;
- ((JComponent)this).firePropertyChange("LineWrap", old, wrap);
- }
-
- public void setRows(int rows) {
- int oldVal = this.rows;
- if (rows < 0) {
- throw new IllegalArgumentException("rows less than zero.");
- } else {
- if (rows != oldVal) {
- this.rows = rows;
- ((Container)this).invalidate();
- }
-
- }
- }
-
- public void setTabSize(int size) {
- Document doc = ((JTextComponent)this).getDocument();
- if (doc != null) {
- int old = this.getTabSize();
- doc.putProperty("tabSize", new Integer(size));
- ((JComponent)this).firePropertyChange("TabSize", old, size);
- }
-
- }
-
- public void setWrapStyleWord(boolean word) {
- boolean old = this.word;
- this.word = word;
- ((JComponent)this).firePropertyChange("WrapStyleWord", old, word);
- }
- }
-